home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / PolyEd / Macros / format.ped < prev    next >
Text File  |  1996-09-26  |  3KB  |  99 lines

  1. /*
  2. ** $VER: format.ped 2.0 (5.3.95) written by Robert Brandner
  3. **
  4. ** This macro formats the current paragraph 'flush-left'
  5. ** (from the current line to the next empty line).
  6. ** It also handles long words and end of file correctly.
  7. */
  8.  
  9. OPTIONS RESULTS
  10.  
  11. OPTIONS FAILAT 11                           /* ignore warnings          */
  12. SIGNAL ON SYNTAX                            /* ensure clean exit        */
  13. SIGNAL ON FAILURE
  14. SIGNAL ON BREAK_C                            /* no label->syntax->exit    */
  15.  
  16. if (LEFT(ADDRESS(), 6) ~= "POLYED") then do
  17.     if SHOW("Ports", "POLYED.1") then
  18.         address 'POLYED.1'
  19.     else do
  20.         say "PolyEd is not running!"
  21.         exit
  22.     end
  23. end
  24.  
  25. 'LOCKGUI'
  26.  
  27. /*----- begin of custom code area --------------------------------------*/
  28.  
  29. 'GETATTR' APPLICATION STEM APP.                /* get all infos on app.    */
  30. 'GETATTR' PROJECT APP.CURRENTPROJECT STEM PROJ. /* get infos on project */
  31. 'GETCURSORPOS' STEM CP.                        /* current line                */
  32.  
  33. /* now we get all lines until we reach an empty one or the end of the
  34. ** text, and join them in a buffer (with a space in between).
  35. ** While doing that, we mark all the lines we put into the buffer.
  36. */
  37. BUFFER = ""
  38. LINECOUNTER = CP.LINE
  39. 'POSITION SOL'
  40. 'BLOCK START'
  41. do forever
  42.     'GETLINE' VAR 'LINE'                    /* get line                    */
  43.     LINE = strip(LINE, B, " "||'09'X)        /* strip spaces and tabs    */
  44.     if length(LINE) = 0 then leave
  45.     BUFFER = BUFFER||" "||LINE                /* add to buffer            */
  46.     'CURSOR DOWN'                            /* move to next line        */
  47.     LINECOUNTER = LINECOUNTER+1                /* increment linecounter    */
  48.     if LINECOUNTER > PROJ.LINES then do        /* leave on end of text        */
  49.         'POSITION EOL'                        /* mark last line too        */
  50.         leave
  51.     end
  52. end
  53. 'ERASE'                                        /* erase the marked area    */
  54.  
  55. /* Now we write out the buffer again. We could make
  56. ** different formats like flush left, flush right, block...
  57. ** To be faster we create a complete line internally
  58. ** instead of writing each single word.
  59. */
  60. line = ""
  61. len = 0
  62. numwords = words(BUFFER)                    /* #words in buffer            */
  63. do i=1 to numwords
  64.     w = word(BUFFER,i)
  65.     wlen = length(w)
  66.     if len = 0 then do                        /* a new line?                */
  67.         line=w
  68.         len=wlen
  69.         iterate i                            /* next word                */
  70.     end
  71.     if len+wlen >= APP.VAR_RIGHTBORDER then do
  72.         'TEXT' line
  73.         'TEXT NEWLINE'
  74.         line = ""
  75.         len = 0
  76.     end
  77.     line = line||' '||w
  78.     len = len+wlen+1                        /* +1 for trailing space    */
  79. end
  80.  
  81. if len > 0 then do                            /* is there a rest?            */
  82.     'TEXT' line                                /* then write it too.        */
  83.     'TEXT NEWLINE'
  84. end
  85.  
  86. /*----- end of custom code area ----------------------------------------*/
  87.  
  88. 'UNLOCKGUI'                                    /* Clean exit                */
  89. EXIT
  90.  
  91. SYNTAX:                                     /* ARexx error...           */
  92. say "Error line" SIGL ":" ERRORTEXT(RC)     /* report it...             */
  93. 'UNLOCKGUI'                                 /* Unlock GUI!              */
  94. EXIT                                        /* exit                     */
  95.  
  96. FAILURE:
  97. 'UNLOCKGUI'                                 /* Unlock GUI!              */
  98. EXIT                                        /* exit                     */
  99.